/****************************************Copyright (c)****************************************************
 
**--------------File Info---------------------------------------------------------------------------------
** File name:                sinewave.h
** Last modified Date:       2024.08.04
** Last Version:             Ver:1.0
** Descriptions:             使用DMA和定时器生成正弦波。通过预先定义的正弦波数据数组和硬件资源的配置，可以
**                           实现对定时器PWM输出的高效控制，从而输出连续的正弦波形。
**                        
**--------------------------------------------------------------------------------------------------------
** Created by:               adm
** Created date:             2024.08.04
** Version:                  Ver:1.0
** Descriptions:             使用DMA和定时器生成正弦波。通过预先定义的正弦波数据数组和硬件资源的配置，可以
**                           实现对定时器PWM输出的高效控制，从而输出连续的正弦波形。
**--------------------------------------------------------------------------------------------------------*/
/*
Copyright (c) [2024]     [BA6IV  ]   [SINEWAVE] is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan
PSL v2.
You may obtain a copy of Mulan PSL v2 at:
         http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY
KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
*/
#include "sinewave.h"
#include <math.h>

/**
*   定义了一个名为sin_data的数组，用于存储正弦波的256个采样点。
*   这些采样点代表了一个完整正弦波周期内的电压值，可以用于模拟正弦波形。
*/
uint16_t sin_data[256];

const float sin_base_data[256] = {
    1.000000f, 1.024541f, 1.049068f, 1.073565f, 1.098017f, 1.122411f, 1.146730f, 1.170962f, 
    1.195090f, 1.219101f, 1.242980f, 1.266713f, 1.290285f, 1.313682f, 1.336890f, 1.359895f, 
    1.382683f, 1.405241f, 1.427555f, 1.449611f, 1.471397f, 1.492898f, 1.514103f, 1.534998f, 
    1.555570f, 1.575808f, 1.595699f, 1.615232f, 1.634393f, 1.653173f, 1.671559f, 1.689541f, 
    1.707107f, 1.724247f, 1.740951f, 1.757209f, 1.773010f, 1.788346f, 1.803208f, 1.817585f, 
    1.831470f, 1.844854f, 1.857729f, 1.870087f, 1.881921f, 1.893224f, 1.903989f, 1.914210f, 
    1.923880f, 1.932993f, 1.941544f, 1.949528f, 1.956940f, 1.963776f, 1.970031f, 1.975702f, 
    1.980785f, 1.985278f, 1.989177f, 1.992480f, 1.995185f, 1.997290f, 1.998795f, 1.999699f, 
    2.000000f, 1.999699f, 1.998795f, 1.997290f, 1.995185f, 1.992480f, 1.989177f, 1.985278f, 
    1.980785f, 1.975702f, 1.970031f, 1.963776f, 1.956940f, 1.949528f, 1.941544f, 1.932993f, 
    1.923880f, 1.914210f, 1.903989f, 1.893224f, 1.881921f, 1.870087f, 1.857729f, 1.844854f, 
    1.831470f, 1.817585f, 1.803208f, 1.788346f, 1.773010f, 1.757209f, 1.740951f, 1.724247f, 
    1.707107f, 1.689541f, 1.671559f, 1.653173f, 1.634393f, 1.615232f, 1.595699f, 1.575808f, 
    1.555570f, 1.534998f, 1.514103f, 1.492898f, 1.471397f, 1.449611f, 1.427555f, 1.405241f, 
    1.382683f, 1.359895f, 1.336890f, 1.313682f, 1.290285f, 1.266713f, 1.242980f, 1.219101f, 
    1.195090f, 1.170962f, 1.146730f, 1.122411f, 1.098017f, 1.073565f, 1.049068f, 1.024541f, 
    1.000000f, 0.975459f, 0.950932f, 0.926435f, 0.901983f, 0.877589f, 0.853270f, 0.829038f, 
    0.804910f, 0.780899f, 0.757020f, 0.733287f, 0.709715f, 0.686318f, 0.663110f, 0.640105f, 
    0.617317f, 0.594759f, 0.572445f, 0.550389f, 0.528603f, 0.507102f, 0.485897f, 0.465002f, 
    0.444430f, 0.424192f, 0.404301f, 0.384768f, 0.365607f, 0.346827f, 0.328441f, 0.310459f, 
    0.292893f, 0.275753f, 0.259049f, 0.242791f, 0.226990f, 0.211654f, 0.196792f, 0.182415f, 
    0.168530f, 0.155146f, 0.142271f, 0.129913f, 0.118079f, 0.106776f, 0.096011f, 0.085790f, 
    0.076120f, 0.067007f, 0.058456f, 0.050472f, 0.043060f, 0.036224f, 0.029969f, 0.024298f, 
    0.019215f, 0.014722f, 0.010823f, 0.007520f, 0.004815f, 0.002710f, 0.001205f, 0.000301f, 
    0.000000f, 0.000301f, 0.001205f, 0.002710f, 0.004815f, 0.007520f, 0.010823f, 0.014722f, 
    0.019215f, 0.024298f, 0.029969f, 0.036224f, 0.043060f, 0.050472f, 0.058456f, 0.067007f, 
    0.076120f, 0.085790f, 0.096011f, 0.106776f, 0.118079f, 0.129913f, 0.142271f, 0.155146f, 
    0.168530f, 0.182415f, 0.196792f, 0.211654f, 0.226990f, 0.242791f, 0.259049f, 0.275753f, 
    0.292893f, 0.310459f, 0.328441f, 0.346827f, 0.365607f, 0.384768f, 0.404301f, 0.424192f, 
    0.444430f, 0.465002f, 0.485897f, 0.507102f, 0.528603f, 0.550389f, 0.572445f, 0.594759f, 
    0.617317f, 0.640105f, 0.663110f, 0.686318f, 0.709715f, 0.733287f, 0.757020f, 0.780899f, 
    0.804910f, 0.829038f, 0.853270f, 0.877589f, 0.901983f, 0.926435f, 0.950932f, 0.975459f
};


/**
  * @brief    正弦波数据设置
  * @param    Period_value: 正弦波频率值
  * @retval   None
  */	
void sin_data_init(uint32_t Period_value)
{
    uint16_t i;
    float d;
    
    for(i=0; i<256; i++) {
        sin_data[i] = (uint16_t)(((float)(PWM_CONSTANT / Period_value) / 2.0f) * sin_base_data[i]);
    }
    sin_freq_set(Period_value);
}


/**
  * @brief    正弦波数据幅值比例设置
  * @param    scale: 正弦波数据幅值比例(按对数曲线设置)
  *           有效范围 0-100，大于100按100取值
  * @retval   None
  */
void sin_data_scale_set(uint8_t scale)
{
    uint16_t i;
    
    if(scale > 100) scale = 100;
    
    for(i=0; i<256; i++) {
        sin_data[i] = sin_data[i] * pow(10.0f, (0.0f - (100.0f - (float)scale)/40.0f));
    }
}

/**
  * @brief    正弦波频率设置
  * @param    freq: 频率值
  * @retval   None
  */
void sin_freq_set(uint32_t freq)
{
    tmr_period_value_set(SPWM_TMR, PWM_CONSTANT/freq);
}

/**
  * @brief    正弦波初始化
  * @param    None
  * @retval   None
  */	
void Sinewave_Init(void)
{
    /* 初始化正弦波数据 */
    sin_data_init(MyKey.tone);
    
    /* 正弦波数据幅值设置 */
    sin_data_scale_set(MyKey.tone_volume);
    
    /* 初始化DMA通道 */
    wk_dma1_channel1_init();
    
    /* 配置DMA通道传输参数 */
    wk_dma_channel_config(DMA1_CHANNEL1, (uint32_t)&TMR1->c1dt/*TMR1->cval*/, DMA1_CHANNEL1_MEMORY_BASE_ADDR, DMA1_CHANNEL1_BUFFER_SIZE);
    dma_channel_enable(DMA1_CHANNEL1, TRUE);
    
    /* 初始化定时器 */
    wk_tmr1_init();
    sin_freq_set(MyKey.tone);
}


